package org.esk.dablog.web.forms;

import org.springframework.web.servlet.mvc.ParameterizableViewController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.InitializingBean;
import org.esk.dablog.service.EntryManager;
import org.esk.dablog.model.forum.ForumTopicEntry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * This class implements "View Entries List" common task
 * User: esk
 * Date: Dec 12, 2006
 * Time: 3:04:25 PM
 * $Id:$
 */
public class ListEntriesForm extends ParameterizableViewController implements InitializingBean {

    private EntryManager entryManager;
    private Class commandClass;
    private String listName = "entries";


    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List entries = entryManager.listEntries(commandClass);
        ModelAndView mav =  super.handleRequestInternal(request, response);
        mav.addObject(listName, entries);
        return mav;
    }


    public void afterPropertiesSet() throws Exception {
        if(commandClass == null) {
            throw new IllegalStateException("commandClass should be set");
        }

    }

    public void setEntryManager(EntryManager entryManager) {
        this.entryManager = entryManager;
    }


    public void setListName(String listName) {
        this.listName = listName;
    }

    public void setCommandClass(Class commandClass) {
        this.commandClass = commandClass;
    }
}
